package com.example.smartcity_ikun_new2.ui.News

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.bumptech.glide.Glide
import com.example.smartcity_ikun_new2.AppContext
import com.example.smartcity_ikun_new2.R
import com.example.smartcity_ikun_new2.databinding.FragmentNewSSBinding
import com.example.smartcity_ikun_new2.databinding.FragmentNewsFramentBinding
import com.example.smartcity_ikun_new2.logic.network.SmartCityNetWorkCreate
import com.example.smartcity_ikun_new2.ui.NewsShow.NewsShowActivity
import com.example.smartcity_ikun_new2.ui.home.HomeViewModel
import com.example.smartcity_ikun_new2.ui.home.NewsFragment.*
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator


class NewSSFragment : Fragment() {


    private val viewModel by lazy {
        ViewModelProviders.of(this)[HomeViewModel::class.java]
    }

    private lateinit var mBinding: FragmentNewSSBinding
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentNewSSBinding.inflate(inflater, container, false)

        setBanner()
        setNewsTitle()

        return mBinding.root
    }

    /**
     *  Banner
     */
    private fun setBanner() {
        val arrayImageUrl = ArrayList<String>()
        viewModel.setGuide(mutableMapOf<String, Int>().apply {
            this["pageNum"] = 1
            this["pageSize"] = 8
            this["type"] = 2
        })
        viewModel.guideLiveData.observe(this, Observer {
            if (it.code == "200") {
                viewModel.guideList.clear()
                viewModel.guideList.addAll(it.rows)
            } else Toast.makeText(context, "加载轮播图出错！", Toast.LENGTH_SHORT).show()

            for (i in viewModel.guideList) arrayImageUrl.add(SmartCityNetWorkCreate.baseUrl + i.advImg)

            // BannerAdapter
            mBinding.homeBanner.apply {
                this.setBannerRound(20F)
                this.addBannerLifecycleObserver(this@NewSSFragment).indicator = CircleIndicator(
                    AppContext.context)
                this.adapter = object : BannerImageAdapter<String>(arrayImageUrl){
                    override fun onBindView(p0: BannerImageHolder?, p1: String?, p2: Int, p3: Int) {
                        Glide.with(context)
                            .load(p1)
                            .into(p0?.imageView!!)
                    }
                }

                // 点击事件
                this.setOnBannerListener{ data, position ->
                    this@NewSSFragment.startActivity(Intent(context, NewsShowActivity::class.java).apply {
                        putExtra("banner", position.toString())
                    })
                }
                this.start()
            }
        })
    }

    /**
     * NewsTitle
     */
    private fun setNewsTitle(){
        val  str = listOf<String>("今日要闻", "专题聚焦", "政策解读", "经济发展", "文化旅游", "科技创新")
        mBinding.homeViewPager2.adapter = object : FragmentStateAdapter(this){
            override fun getItemCount() = 6
            override fun createFragment(position: Int): Fragment = when(position){
                0 -> NewsFrament()
                1 -> NewsFragment2()
                2 -> NewsFragment3()
                3 -> NewsFragment4()
                4 -> NewsFragment5()
                else -> NewsFragment6()
            }
        }

        mBinding.homeTabLayout.tabMode = TabLayout.MODE_SCROLLABLE
        TabLayoutMediator(mBinding.homeTabLayout, mBinding.homeViewPager2){tab, position ->
            when(position){
                0 -> tab.text = str[0]
                1 -> tab.text = str[1]
                2 -> tab.text = str[2]
                3 -> tab.text = str[3]
                4 -> tab.text = str[4]
                else -> tab.text = str[5]
            }
        }.attach()
    }
}